package com.kfm.pm.split;

import lombok.SneakyThrows;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class SplitThread {

    public static void main(String[] args) {

        String fileName = "io\\dj.mp4";
        String dest = "io\\cutter";
        for (int i = 0; i < 10; i++) {
            Thread thread = new Thread(() -> {
                split(fileName, dest, 1024 * 1024);
            });
            thread.start();
        }
        // 123 456 789 in.readBytes()   0-8192  0-1024 1024-2048 2048 - 3072 3072-4096
        // int count = 0
        // for (int i = 0; i < bytes.length(); i += 1024 * 1024) {
        //       new (bytes, i, i + 1024*1024, ++count).start();
        // }


    }

    @SneakyThrows
    private static void split(String fileName, String dest, int size) {
        File file = new File(fileName);
        File destFile = new File(dest);
        if (!destFile.exists()) {
            destFile.mkdirs();
        }
        try (FileInputStream in = new FileInputStream(file);) {
            int count = 0;
            while (in.available() > 0) {
                byte[] bytes = new byte[size];
                FileOutputStream out = new FileOutputStream(new File(destFile, ++count + ".tmp"));
                int read = in.read(bytes);
                out.write(bytes, 0, read);
            }
        }

    }
}
